<?php

namespace frontend\modules\yii2\widgets\layouts\sidebar;

use frontend\modules\yii2\widgets\layouts\sidebar\assets\SidebarAsset;
use Yii;
use yii\base\Widget;
use yii\bootstrap\NavBar;

/**
 * A menu imitate the sidebar of https://docs.docker.com/get-started/
 *
 * @note It's depend on bootstrap;
 *
 * @example :
 * ```php

 * ```
 * @author chenzhiwei <chenzhiweiwk@qq.com>
 * @date   2021/08/31
 */
class Sidebar extends Widget
{

    public $items = [];

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        SidebarAsset::register($this->getView());
    }

    private function getItemHtml($items, $level = 1){
        $html = "";
        foreach ($items as $item){
            if(isset($item["items"])) {
                $id = "items_" . uniqid();
                $html .= '<li><a href="'. $item["url"] .'" class="collapsed" data-toggle="collapse" data-target="#'.$id.'" aria-expanded="false" aria-controls="'.$id.'"><span>'. $item["label"] .'</span><span class="caret"></span></a>';
                $html .= '<ul class="nav nav-sidebar collapse level'. ($level + 1) .'" id="'. $id .'">';
                $html .= $this->getItemHtml($item["items"], $level + 1);
                $html .= "</ul>";
            } else {
                $html .= "<li><a href='$item[url]'>$item[label]</a>";
            }
            $html .= "</li>";
        }
        return $html;
    }


    public function run()
    {
        $html =  '<ul class="nav nav-sidebar">' . $this->getItemHtml($this->items) . "</ul>";
        return $html;
    }


}
